package com.chenqi.tool.pop

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.view.Gravity
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.PopupWindow
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chenqi.tool.R
import com.chenqi.tool.dialog.FootListDialogAdapter
import com.chenqi.tool.recycleview.base.OnItemClickListener

/**
 * 作者: CQ
 * 日期: 2021-06-11
 * 说明:
 */
class FootPopupWindow(context: Context) : PopupWindow(context) {
    class Builder(context: Context) {
        private var context: Context? = context
        private var list: MutableList<String>? = null
        private var onItemClickListener: OnItemClickListener? = null

        fun setData(list: MutableList<String>): Builder {
            this.list = list
            return this
        }

        fun setOnItemClickListener(onItemClickListener: OnItemClickListener): Builder {
            this.onItemClickListener = onItemClickListener
            return this
        }

        @RequiresApi(Build.VERSION_CODES.KITKAT)
        fun create(): FootPopupWindow {
            val footPopupWindow = FootPopupWindow(context!!)
            footPopupWindow.width = ViewGroup.LayoutParams.MATCH_PARENT
            footPopupWindow.height = ViewGroup.LayoutParams.WRAP_CONTENT

            val view = LayoutInflater.from(context).inflate(R.layout.foot_list_dialog, null)

            val recyclerView = view.findViewById<RecyclerView>(R.id.foot_popup_recyclerView)
            val linearLayoutManager = LinearLayoutManager(context)
            linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
            linearLayoutManager.isAutoMeasureEnabled = true
            recyclerView.addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL))
            recyclerView.layoutManager = linearLayoutManager // 设置线行垂直布局
            val footPopupAdapter = FootListDialogAdapter(context!!, list!!)// 定义适配器并使用
            recyclerView.adapter = footPopupAdapter

            footPopupWindow.contentView = view// 设置视图
            footPopupWindow.isOutsideTouchable = true// 设置外部可点击
            footPopupWindow.isFocusable = true// 设置弹出窗体可点击
            footPopupWindow.setBackgroundDrawable(ColorDrawable(Color.GRAY))//设置背景，点击back可消失
            footPopupWindow.animationStyle = R.style.popWindow_animation_anim//设置弹出动画
            footPopupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0)//PopupWindow的显示及位置设置

            view.findViewById<TextView>(R.id.foot_popup_bottom)
                .setOnClickListener { footPopupWindow.dismiss() }

            if (onItemClickListener != null) {
                footPopupAdapter.setOnItemClickListener(onItemClickListener!!)// 设置监听
            }
            return footPopupWindow
        }
    }

}